package vcp.server.deamontasks;

import java.util.TimerTask;

import org.joda.time.DateTime;

import vcp.entity.WeekStatistics;
import vcp.server.DAO.WeekStatisticsDAO;
import vcp.server.interfaces.IServer;
import vcp.server.utility.Constants.TableNames;
import vcp.server.utility.SystemClock;

/**
 * 
 * @author VCP team
 *         <p>
 *         class that runs every end of week and runs statistics
 *         </p>
 */
public class EndWeekStatisticsTask extends TimerTask
{
	private final IServer server;

	/**
	 * class constructor
	 * 
	 * @param serverDeamon
	 */
	public EndWeekStatisticsTask ( IServer serverDeamon )
	{
		this.server = serverDeamon;
	}

	@Override
	public void run ()
	{
		synchronized (this.server.getDBLock())
		{
			System.out.println("EndOfSubscriptionTask:  TrueTime:" + new DateTime() + "  SysTime:" + SystemClock.getClockInstance().getSystemDateTime());
			WeekStatisticsDAO weekStatisticsDAO = new WeekStatisticsDAO();

			DateTime startWeek = SystemClock.getClockInstance().getSystemDateTime().minusDays(7);
			DateTime endWeek = SystemClock.getClockInstance().getSystemDateTime();

			WeekStatistics weekStatistics = new WeekStatistics();
			weekStatistics.setID(this.server.getGeneratedID(TableNames.WEEKLY_STATISTICS));
			weekStatistics.setStartDate(startWeek);
			weekStatistics.setEndDate(endWeek);

			weekStatisticsDAO.save(weekStatistics);
		}
	}
}
